//
//  DWDPicView.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/13.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit
import SDWebImage
let DWDStatusPictureViewCellID = "DWDStatusPictureViewCellID"
class DWDPicView: UICollectionView {
    
    var statusModel:DWDStatusModel? {
        didSet{
            //            sizeToFit会调用sizeThatFits重写sizeThatFits方法设置模型的大小
            sizeToFit()
            //            解决了cell的重用问题
            reloadData()
        }
    }
    override func sizeThatFits(size: CGSize) -> CGSize {
        return setUpSize()
    }
    //            根据图片数量设置picView大小,和itemSize
    func setUpSize() ->CGSize{
        let count = self.statusModel?.thumbnailUrls?.count ?? 0
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: DWDPicViewWidth, height: DWDPicViewWidth)
        if count == 0 {
            return CGSizeZero
        }
        if count == 1 {
            var size = CGSize(width: 200, height: 150)
            let key = statusModel!.thumbnailUrls![0].absoluteString
            if let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key){
            size = image.size
            }
            size.width = size.width < 40 ? 40:size.width
            size.width = size.width > 300 ? 300:size.width
            layout.itemSize = size
            return size
        }
        if count == 4 {
            return CGSize(width: DWDPicViewWidth * 2 + DWDPicViewMargin, height: DWDPicViewWidth * 2 + DWDPicViewMargin)
        }
        let row = CGFloat((count - 1) / Int(DWDPicViewCount) + 1)
        let h = row * DWDPicViewWidth + (row - 1) * DWDPicViewMargin
        let w = DWDPicViewMaxWidth
        return CGSize(width: w, height: h)
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        backgroundColor = UIColor.clearColor()
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumInteritemSpacing = DWDPicViewMargin
        layout.minimumLineSpacing = DWDPicViewMargin
        //        让自己成为数据源
        dataSource = self
        registerClass(DWDPicViewCell.self, forCellWithReuseIdentifier: DWDStatusPictureViewCellID)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//数据源方法
extension DWDPicView:UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return statusModel?.thumbnailUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(DWDStatusPictureViewCellID, forIndexPath: indexPath) as! DWDPicViewCell
        cell.imageUrl = statusModel!.thumbnailUrls![indexPath.item]
        return cell
    }
}
//自定义cell
private class DWDPicViewCell:UICollectionViewCell {
    //      设置url同时给iconView赋值
    var imageUrl:NSURL? {
        didSet{
            iconView.sd_setImageWithURL(imageUrl)
            iconView.contentMode = .ScaleAspectFill
            iconView.clipsToBounds = true
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconView)
        iconView.ff_Fill(self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let iconView = UIImageView()
}